package com.example.myapplication;

import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


import com.example.androiduidemo.R;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;


public class updatePassword extends AppCompatActivity {
    private EditText editTextUsername, editTextOldPassword, editTextNewPassword;
    private Button buttonUpdate;
    private MySQLiteOpenHelper1 dbHelper;

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Compiler EdgeToEdge = null;
        EdgeToEdge.enable();
        setContentView(R.layout.activity_update_password);
        editTextUsername = findViewById(R.id.editTextUsername);
        editTextOldPassword = findViewById(R.id.editTextOldPassword);
        editTextNewPassword = findViewById(R.id.editTextNewPassword);
        buttonUpdate = findViewById(R.id.buttonUpdate);
        dbHelper = new MySQLiteOpenHelper1(this);
        buttonUpdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username = editTextUsername.getText().toString().trim();
                String oldPassword = editTextOldPassword.getText().toString().trim();
                String newPassword = editTextNewPassword.getText().toString().trim();

                if (!username.isEmpty() && !oldPassword.isEmpty() && !newPassword.isEmpty()) {
                    updateUserPassword(username, oldPassword, newPassword);
                } else {
                    Toast.makeText(updatePassword.this, "请填写所有字段", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private void updateUserPassword(String username, String oldPassword, String newPassword) {
        if (!username.isEmpty() && !oldPassword.isEmpty() && !newPassword.isEmpty()) {
            // 检查新密码是否与旧密码相同
            if (oldPassword.equals(newPassword)) {
                Toast.makeText(updatePassword.this, "新密码不能与旧密码相同，请重新输入", Toast.LENGTH_SHORT).show();
                return; // 直接返回，不执行后续的密码更新操作
            }

            // 弹出确认修改对话框
            new AlertDialog.Builder(this)
                    .setTitle("确认修改")
                    .setMessage("您确定要修改密码吗？")
                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            if (dbHelper.login(username, oldPassword)) {
                                dbHelper.updatePassword(username, newPassword);
                                Toast.makeText(updatePassword.this, "密码已更新", Toast.LENGTH_SHORT).show();
                                Intent intent = new Intent(updatePassword.this, Login.class); // 确保LoginActivity类名正确
                                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
                                startActivity(intent);
                                finish(); // 结束当前Activity
                            } else {
                                Toast.makeText(updatePassword.this, "旧密码错误，请重试", Toast.LENGTH_SHORT).show();
                            }
                        }
                    })
                    .setNegativeButton("取消", null)
                    .show();
        } else {
            Toast.makeText(this, "请填写所有字段", Toast.LENGTH_SHORT).show();
        }
    }
}